<?php

namespace App\Service\Agency;

use App\Model\Agency\AgencyCompanyModel;
use App\Repository\Contro\AgencyCompanyContro;
use App\Repository\Msq\AgencyCompanyMsq;
use App\Repository\Redis\AgencyCompanyRedis;
use App\Service\ServiceGeneral;
use Hyperf\Di\Annotation\Inject;

class AgencyCompanyService extends ServiceGeneral
{
    #[Inject]
    public AgencyCompanyContro $agencyCompanyContro;
    #[Inject]
    public AgencyCompanyMsq $agencyCompanyMsq;
    #[Inject]
    public AgencyCompanyRedis $agencyCompanyRedis;

    public function addAgencyCompany(array $data){
        $info = $this->agencyCompanyContro->getAgencyCompanyByName($data['agency_company_name'],["id"]);
        if($info){
            apiError("当前委案公司已存在");
        }

        $data = array_merge($data,[
            'organ_id' => getOrganIdByOrganCode(),
            'status' => 1,
            'created_at' => date("Y-m-d H:i:s"),
            'updated_at' => date("Y-m-d H:i:s"),
        ]);

        $res = $this->agencyCompanyContro->addAgencyCompany($data);
        $this->agencyCompanyRedis->updateCompanyCache();
        return $res;
    }

    public function getAgencyCompanyList($filters = []) :mixed{
        $page = $this->request->post('page_size',1);
        return $this->agencyCompanyContro->getAgencyCompanyList($page,$filters);
    }

    public function getAgencyCompanyInfo(int $id) :mixed {
        return $this->agencyCompanyContro->getAgencyCompanyInfo($id);
    }

    public function delAgencyCompany(array $ids)
    {
        try {
            $res = $this->agencyCompanyContro->delAgencyCompanyByIds($ids);
            $this->agencyCompanyRedis->updateCompanyCache();
        }catch (\Exception $exception){
            apiError("删除异常");
        }
        return $res;
    }

    public function editAgencyCompany(string $id,array $data){
        $res = $this->agencyCompanyContro->editAgencyCompany($id,$data);
        $this->agencyCompanyRedis->updateCompanyCache();
        return $res;
    }

    public function getAgencyCompanySelect(){
        $select = $this->agencyCompanyRedis->getAgencyCompanySelect();
        if(empty($select)){
            $select = $this->agencyCompanyMsq->getAgencyCompanySelect();
            $this->agencyCompanyRedis->updateCompanyCache();
        }
        return $select;
    }



}